package com.itfreer.power.dao.role;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

import com.itfreer.form.api.JdbcBaseEntity;
import com.itfreer.power.dao.base.JdbcEntityBase;
import com.itfreer.power.entity.role.RoleDataJurisdictionEntity;
import com.itfreer.power.entity.role.RoleEntity;
import com.itfreer.power.entity.role.RoleMapExtentJurisdictionEntity;
import com.itfreer.power.entity.role.RoleMapLayerJurisdictionEntity;
import com.itfreer.power.entity.role.RoleMenuJurisdictionEntity;
import com.itfreer.power.entity.role.RoleServiceJurisdictionEntity;

/**
 * 定义角色实体类
 */
@Entity(name = "sso_role")
public class JdbcRoleEntity extends JdbcEntityBase implements JdbcBaseEntity<RoleEntity> {

	private static final long serialVersionUID = -3994919371092668844L;
	
	/**
	 * 分组
	 */
	@Column(name = "s_groupName", nullable = true)
	private String groupName;
	
	/**
	 * 是否是用户注册时的默认角色
	 * 只应用于用户注册接口
	 */
	@Column(name = "b_urdr", nullable = true)
	private Boolean isUserRegisterDefulatRole;
	
	/**
	 * 当用户没有挂角色时，默认挂此角色
	 */
	@Column(name = "b_udr", nullable = true)
	private Boolean isUserDefulatRole;
	
	/**
	 * 应用服务授权特有角色
	 * 应用于通过接口授权可访问指定应用时，注册或取消此角色
	 */
	@Column(name = "s_registerService", nullable = true)
	private String registerService;

	/**
	 * 菜单权限
	 */
	@SuppressWarnings("deprecation")
	@org.hibernate.annotations.ForeignKey(name = "none")
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "role_id")
	private Set<JdbcRoleMenuJurisdictionEntity> menus = new HashSet<JdbcRoleMenuJurisdictionEntity>();

	/**
	 * 数据权限
	 */
	@SuppressWarnings("deprecation")
	@org.hibernate.annotations.ForeignKey(name = "none")
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "role_id")
	private Set<JdbcRoleDataJurisdictionEntity> datas = new HashSet<JdbcRoleDataJurisdictionEntity>();

	/**
	 * 图层权限
	 */
	@SuppressWarnings("deprecation")
	@org.hibernate.annotations.ForeignKey(name = "none")
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "role_id")
	private Set<JdbcRoleMapLayerJurisdictionEntity> mapLayers = new HashSet<JdbcRoleMapLayerJurisdictionEntity>();

	/**
	 * 地图范围
	 */
	@SuppressWarnings("deprecation")
	@org.hibernate.annotations.ForeignKey(name = "none")
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "role_id")
	private Set<JdbcRoleMapExtentJurisdictionEntity> mapExtents = new HashSet<JdbcRoleMapExtentJurisdictionEntity>();

	/**
	 * 应用服务
	 */
	@SuppressWarnings("deprecation")
	@org.hibernate.annotations.ForeignKey(name = "none")
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "role_id")
	private Set<JdbcRoleServiceJurisdictionEntity> services = new HashSet<JdbcRoleServiceJurisdictionEntity>();

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public Set<JdbcRoleMenuJurisdictionEntity> getMenus() {
		return menus;
	}

	public void setMenus(Set<JdbcRoleMenuJurisdictionEntity> menus) {
		this.menus = menus;
	}

	public Set<JdbcRoleDataJurisdictionEntity> getDatas() {
		return datas;
	}

	public void setDatas(Set<JdbcRoleDataJurisdictionEntity> datas) {
		this.datas = datas;
	}

	public Set<JdbcRoleMapLayerJurisdictionEntity> getMapLayers() {
		return mapLayers;
	}

	public void setMapLayers(Set<JdbcRoleMapLayerJurisdictionEntity> mapLayers) {
		this.mapLayers = mapLayers;
	}

	public Set<JdbcRoleMapExtentJurisdictionEntity> getMapExtents() {
		return mapExtents;
	}

	public void setMapExtents(Set<JdbcRoleMapExtentJurisdictionEntity> mapExtents) {
		this.mapExtents = mapExtents;
	}

	public Set<JdbcRoleServiceJurisdictionEntity> getServices() {
		return services;
	}

	public void setServices(Set<JdbcRoleServiceJurisdictionEntity> services) {
		this.services = services;
	}


	@Override
	public void from(RoleEntity entity) {
		this.setAbbreviation(entity.getAbbreviation());
		this.setCode(entity.getCode());
		this.setDisplay(entity.getDisplay());
		this.setId(entity.getId());
		this.setInfo(entity.getInfo());
		this.setOrder(entity.getOrder());
		this.setValid(entity.getValid());
		this.setGroupName(entity.getGroupName());
		this.setIsUserDefulatRole(entity.getIsUserDefulatRole());
		this.setIsUserRegisterDefulatRole(entity.getIsUserRegisterDefulatRole());
		this.setRegisterService(entity.getRegisterService());

		Set<JdbcRoleMenuJurisdictionEntity> menus = new HashSet<JdbcRoleMenuJurisdictionEntity>();
		Set<RoleMenuJurisdictionEntity> menusItems = entity.getMenus();
		if (menusItems != null) {
			for (RoleMenuJurisdictionEntity item : menusItems) {
				JdbcRoleMenuJurisdictionEntity jm = new JdbcRoleMenuJurisdictionEntity();
				jm.from(item);
				menus.add(jm);
			}
		}
		this.setMenus(menus);

		Set<JdbcRoleDataJurisdictionEntity> datas = new HashSet<JdbcRoleDataJurisdictionEntity>();
		Set<RoleDataJurisdictionEntity> datasItems = entity.getDatas();
		if (datasItems != null) {
			for (RoleDataJurisdictionEntity item : datasItems) {
				JdbcRoleDataJurisdictionEntity jd = new JdbcRoleDataJurisdictionEntity();
				jd.from(item);
				datas.add(jd);
			}
		}
		this.setDatas(datas);

		Set<JdbcRoleMapLayerJurisdictionEntity> mapLayers = new HashSet<JdbcRoleMapLayerJurisdictionEntity>();
		Set<RoleMapLayerJurisdictionEntity> mapLayersItems = entity.getMapLayers();
		if (mapLayersItems != null) {
			for (RoleMapLayerJurisdictionEntity item : mapLayersItems) {
				JdbcRoleMapLayerJurisdictionEntity jml = new JdbcRoleMapLayerJurisdictionEntity();
				jml.from(item);
				mapLayers.add(jml);
			}
		}
		this.setMapLayers(mapLayers);

		Set<JdbcRoleMapExtentJurisdictionEntity> extents = new HashSet<JdbcRoleMapExtentJurisdictionEntity>();
		Set<RoleMapExtentJurisdictionEntity> extentsItems = entity.getMapExtents();
		if (extentsItems != null) {
			for (RoleMapExtentJurisdictionEntity item : extentsItems) {
				JdbcRoleMapExtentJurisdictionEntity jme = new JdbcRoleMapExtentJurisdictionEntity();
				jme.from(item);
				extents.add(jme);
			}
		}
		this.setMapExtents(extents);

		Set<JdbcRoleServiceJurisdictionEntity> services = new HashSet<JdbcRoleServiceJurisdictionEntity>();
		Set<RoleServiceJurisdictionEntity> servicesItems = entity.getServices();
		if (servicesItems != null) {
			for (RoleServiceJurisdictionEntity item : servicesItems) {
				JdbcRoleServiceJurisdictionEntity jsj = new JdbcRoleServiceJurisdictionEntity();
				jsj.from(item);
				services.add(jsj);
			}
		}
		this.setServices(services);
	}
	

	@Override
	public RoleEntity toEntity() {
		RoleEntity toEntity = new RoleEntity();
		toEntity.setAbbreviation(this.getAbbreviation());
		toEntity.setCode(this.getCode());
		toEntity.setDisplay(this.getDisplay());
		toEntity.setId(this.getId());
		toEntity.setInfo(this.getInfo());
		toEntity.setOrder(this.getOrder());
		toEntity.setValid(this.getValid());
		toEntity.setGroupName(this.getGroupName());
		toEntity.setIsUserDefulatRole(this.getIsUserDefulatRole());
		toEntity.setIsUserRegisterDefulatRole(this.getIsUserRegisterDefulatRole());
		toEntity.setRegisterService(this.getRegisterService());

		Set<RoleMenuJurisdictionEntity> menus = new HashSet<RoleMenuJurisdictionEntity>();
		Set<JdbcRoleMenuJurisdictionEntity> menusItems = this.getMenus();
		if (menusItems != null) {
			for (JdbcRoleMenuJurisdictionEntity item : menusItems) {
				menus.add(item.toEntity());
			}
		}
		toEntity.setMenus(menus);

		Set<RoleDataJurisdictionEntity> datas = new HashSet<RoleDataJurisdictionEntity>();
		Set<JdbcRoleDataJurisdictionEntity> datasItems = this.getDatas();
		if (datasItems != null) {
			for (JdbcRoleDataJurisdictionEntity item : datasItems) {
				datas.add(item.toEntity());
			}
		}
		toEntity.setDatas(datas);

		Set<RoleMapLayerJurisdictionEntity> mapLayers = new HashSet<RoleMapLayerJurisdictionEntity>();
		Set<JdbcRoleMapLayerJurisdictionEntity> mapLayersItems = this.getMapLayers();
		if (mapLayersItems != null) {
			for (JdbcRoleMapLayerJurisdictionEntity item : mapLayersItems) {
				mapLayers.add(item.toEntity());
			}
		}
		toEntity.setMapLayers(mapLayers);

		Set<RoleMapExtentJurisdictionEntity> extents = new HashSet<RoleMapExtentJurisdictionEntity>();
		Set<JdbcRoleMapExtentJurisdictionEntity> extentsItems = this.getMapExtents();
		if (extentsItems != null) {
			for (JdbcRoleMapExtentJurisdictionEntity item : extentsItems) {
				extents.add(item.toEntity());
			}
		}
		toEntity.setMapExtents(extents);

		Set<RoleServiceJurisdictionEntity> services = new HashSet<RoleServiceJurisdictionEntity>();
		Set<JdbcRoleServiceJurisdictionEntity> servicesItems = this.getServices();
		if (servicesItems != null) {
			for (JdbcRoleServiceJurisdictionEntity item : servicesItems) {
				services.add(item.toEntity());
			}
		}
		toEntity.setServices(services);

		return toEntity;
	}

	public Boolean getIsUserRegisterDefulatRole() {
		return isUserRegisterDefulatRole;
	}

	public void setIsUserRegisterDefulatRole(Boolean isUserRegisterDefulatRole) {
		this.isUserRegisterDefulatRole = isUserRegisterDefulatRole;
	}

	public Boolean getIsUserDefulatRole() {
		return isUserDefulatRole;
	}

	public void setIsUserDefulatRole(Boolean isUserDefulatRole) {
		this.isUserDefulatRole = isUserDefulatRole;
	}

	public String getRegisterService() {
		return registerService;
	}

	public void setRegisterService(String registerService) {
		this.registerService = registerService;
	}
}
